/**
 * 
 */
package vista.ventanas;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JPanel;

import business.Controlador;

/**
 * @author De La
 *
 */
public class PanelPizarra extends JPanel {
	
	private static PanelPizarra instance;
	
	private Boolean dibujando = false;
	private Boolean nuevoTrazo = false;
	private Point lastPosition;
	
	/**
	 * 
	 *
	 */
	private PanelPizarra() {
		setBackground( new Color(180,200,180) );
		addMouseMotionListener( new MouseMotionListener() );
		addMouseListener( new MouseListener() );
		//Creo el cursor.
		Image img = Toolkit.getDefaultToolkit().getImage("imagenes/puntero.gif");
		Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0,0), "punto");
		setCursor( cursor );
	}
	
	/**
	 * 
	 *
	 */
	private static synchronized void createInstance() {
		if ( instance == null ) {
			instance = new PanelPizarra();
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public static PanelPizarra getInstance()  {
		if ( instance == null ) {
			createInstance();
		}
		return instance;
	}
	
	private class MouseListener extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent e) {
			PanelPizarra.getInstance().setDibujando( !PanelPizarra.getInstance().getDibujando() );
			if ( PanelPizarra.getInstance().getDibujando() ) {
				PanelPizarra.getInstance().setNuevoTrazo(true);
			}
		}
		@Override
		public void mouseExited(MouseEvent e) {
			if ( !PanelPizarra.getInstance().getDibujando() ) {
				setLastPosition( null );
			}
		}
		
		@Override
		public void mouseEntered(MouseEvent e) {
			if ( getLastPosition() != null ) {
				try {
					new Robot().mouseMove( getLocationOnScreen().x + getLastPosition().x
							, getLocationOnScreen().y + getLastPosition().y );
				} catch (AWTException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
	
	private class MouseMotionListener extends MouseMotionAdapter {
		@Override
		public void mouseMoved(MouseEvent e) {
			if ( getDibujando() && (getMousePosition() != null) ) {
				Controlador.getInstance().addPunto( getMousePosition(), getNuevoTrazo() );
				setLastPosition( new Point( getMousePosition() ) );
				setNuevoTrazo(false);
			}
		}
	}

	/**
	 * @return the dibujando
	 */
	public Boolean getDibujando() {
		return dibujando;
	}

	/**
	 * @param dibujando the dibujando to set
	 */
	public void setDibujando(Boolean dibujando) {
		this.dibujando = dibujando;
	}

	/**
	 * @return the nuevoTrazo
	 */
	public Boolean getNuevoTrazo() {
		return nuevoTrazo;
	}

	/**
	 * @param nuevoTrazo the nuevoTrazo to set
	 */
	public void setNuevoTrazo(Boolean nuevoTrazo) {
		this.nuevoTrazo = nuevoTrazo;
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for ( int i=0; i<getComponents().length ; i++ ) {
			getComponents()[i].paint(g);
		}
	}

	/**
	 * @return the lastPosition
	 */
	public Point getLastPosition() {
		return lastPosition;
	}

	/**
	 * @param lastPosition the lastPosition to set
	 */
	public void setLastPosition(Point lastPosition) {
		this.lastPosition = lastPosition;
	}
}
